import { Util } from '../../utils/util.js';
import { Store } from '../store/store.js';
import { StoreRetailSettings } from './storeRetailSettings.js';
import { Engine } from '../engine.js';

export class MerchantRetailSettings {
    constructor({
        singleCommodityDiscount = true,
        orderDiscout = true,
        orderPromotion = true,
        withoutSmallCharge = true,
        cashRegisterMode = false,
        storeSettingList = [],
    } = {}) {
        this.singleCommodityDiscount = singleCommodityDiscount;
        this.orderDiscout = orderDiscout;
        this.orderPromotion = orderPromotion;
        this.withoutSmallCharge = withoutSmallCharge;
        this.cashRegisterMode = cashRegisterMode;
        this.storeRetailInventorySettingsList = storeSettingList;
    }



    validateSettingData({ publicStoreId = null }) {
        if (publicStoreId != null) {
            const storeSettings = this.getStoreRetailInventorySettings(Store.empty({ id: publicStoreId }));
            return storeSettings.validateSettingData();
        } else {
            for (const item of this.storeRetailInventorySettingsList) {
                const error = item.validateSettingData();
                if (error != null) {
                    return error;
                }
            }
        }
        return null;
    }

    getStoreRetailInventorySettings(store) {
        const found = this.storeRetailInventorySettingsList.find(
            (element) => element.store.id === store.id
        );
        if (found) return found;

        const newSetting = new StoreRetailSettings({ store });
        this.storeRetailInventorySettingsList.push(newSetting);
        return newSetting;
    }

    _$MerchantRetailSettingsToJson({ publicStoreId = null }) {
        const ret = {
            singleCommodityDiscount: this.singleCommodityDiscount ? 1 : 0,
            orderDiscout: this.orderDiscout ? 1 : 0,
            orderPromotion: this.orderPromotion ? 1 : 0,
            withoutSmallCharge: this.withoutSmallCharge ? 1 : 0,
            cashRegisterMode: this.cashRegisterMode ? 1 : 0,
            storeRetailInfoList: [],
        };

        if (publicStoreId != null) {
            const storeSettings = this.getStoreRetailInventorySettings(Store.empty({ id: publicStoreId }));
            ret.storeRetailInfoList.push(storeSettings.toJson());
        } else {
            for (const item of this.storeRetailInventorySettingsList) {
                ret.storeRetailInfoList.push(item.toJson());
            }
        }

        return ret;
    }

    toJson() {
        const publicStoreId = Engine.getOrgSettings().multiStoreMode
            ? null
            : Engine.profile?.org?.publicStore?.id;
        return this._$MerchantRetailSettingsToJson({ publicStoreId });
    }

    static fromJson(json) {
        const instance = new MerchantRetailSettings({
            singleCommodityDiscount: Util.jsonToBool(json["singleCommodityDiscount"], true),
            orderDiscout: Util.jsonToBool(json["orderDiscout"], true),
            orderPromotion: Util.jsonToBool(json["orderPromotion"], true),
            withoutSmallCharge: Util.jsonToBool(json["withoutSmallCharge"], true),
            cashRegisterMode: Util.jsonToBool(json["cashRegisterMode"], false),
            storeSettingList: [],
        });

        const storeSettingList = json["storeRetailInfoList"];
        if (storeSettingList != null) {
            for (const item of storeSettingList) {
                const setting = StoreRetailSettings.fromJson(item);
                instance.storeRetailInventorySettingsList.push(setting);
            }
        }

        return instance;
    }
}
